module.exports = {
  /**
	 * 角色模块
   * 权限标识：
   *  - READ_ROLE
   *  - ADD_ROLE
   *  - DELETE_ROLE
   *  - UPDATE_ROLE
	 */

  /**
   * 获取角色列表
   */
  async getRoles() {
    const { body = {} } = this.getHttpInfo()
    const { params, clientInfo } = JSON.parse(body)

    const skip = params.pageNum == 1 ? 0 : params.pageSize * (params.pageNum - 1)
    const res = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-roles')
      .skip(skip)
      .limit(params.pageSize)
      .get({
        getCount: true
      })

    return res
  },

  /**
   * 添加角色
   */
  async addRole() {
    const { body = {} } = this.getHttpInfo()
    const { params, clientInfo } = JSON.parse(body)

    // 检查必要字段
    if (!params || !params.role_id || !params.role_name) {
      return {
        errCode: 400,
        message: '角色标识和角色名称不能为空'
      }
    }

    // 检查角色ID是否已存在
    const existRole = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-roles')
      .where({ role_id: params.role_id })
      .count()

    if (existRole.total > 0) {
      return {
        errCode: 400,
        message: '角色标识已存在'
      }
    }

    //剔除报错字段
    delete params._id
    // 插入数据
    const res = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-roles')
      .add(params)

    return res
  },

  /**
   * 修改角色
   */
  async updateRole() {
    const { body = {} } = this.getHttpInfo()
    const { params, clientInfo } = JSON.parse(body)
    const _id = params._id

    if (!_id) {
      return {
        errCode: 400,
        message: '角色ID不能为空'
      }
    }

    delete params._id
    const res = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-roles')
      .where({ _id })
      .update(params)

    if (res.updated === 0) {
      return {
        errCode: 500,
        message: '角色更新失败'
      }
    }

    return res
  },

  /**
   * 修改角色菜单
   */
  async updateRoleMenu() {
    const { body = {} } = this.getHttpInfo()
    const { params, clientInfo } = JSON.parse(body)
    const _id = params._id

    delete params._id
    const res = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-roles')
      .where({ _id })
      .update(params)

    return res
  },

  /**
   * 删除角色
   */
  async deleteRole() {
    const { body = {} } = this.getHttpInfo()
    const { params, clientInfo } = JSON.parse(body)
    if (!params._id) {
      return {
        errCode: 400,
        message: '角色ID不能为空'
      }
    }

    // 检查角色是否被用户使用
    const userCount = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-users')
      .where({
        role: params.role_name
      })
      .count()

    if (userCount.total > 0) {
      return {
        errCode: 400,
        message: '当前角色已被用户使用，无法删除'
      }
    }

    const res = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-roles')
      .where({ _id: params._id })
      .remove()

    if (res.deleted === 0) {
      return {
        errCode: 404,
        message: '角色不存在'
      }
    }

    return res
  }
}
